<template>
  <div>
    <el-table :data="books" style="width: 100%">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="title" label="书名"></el-table-column>
      <el-table-column prop="author" label="作者"></el-table-column>
      <el-table-column prop="copies" label="库存"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.$index, scope.row)" type="danger" size="mini">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button @click="showAddDialog" type="primary">添加图书</el-button>
    <el-dialog title="Add New Book" :visible.sync="dialogVisible">
      <el-form :model="newBook" label-width="80px">
        <el-form-item label="书名">
          <el-input v-model="newBook.title"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="newBook.author"></el-input>
        </el-form-item>
        <el-form-item label="库存">
          <el-input v-model="newBook.copies"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addBook">添加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      books: [
        {"id": 1, "title": "Python编程", "author": "John Doe", "copies": 4},
        {"id": 2, "title": "历史的秘密", "author": "Jane Smith", "copies": 3},
        // ... 其他图书数据
      ],
      dialogVisible: false,
      newBook: {
        title: '',
        author: '',
        copies: 0
      }
    };
  },
  methods: {
    handleDelete(index, row) {
      this.books.splice(index, 1);
    },
    showAddDialog() {
      this.dialogVisible = true;
    },
    addBook() {
      this.books.push({
        id: this.books.length + 1,
        title: this.newBook.title,
        author: this.newBook.author,
        copies: this.newBook.copies
      });
      this.dialogVisible = false;
      this.newBook = {
        title: '',
        author: '',
        copies: 0
      };
    }
  }
};
</script>
